// src/services/borrowServiceFactory.js
import { BorrowRecord } from '@/models/BorrowRecord'
import api from '@/services/api'

export const createBorrowService = () => {
  console.log('🔌 使用真实借阅API模式')

  return {
    async borrowBook(bookId, borrowerName, borrowerPhone) {
      const response = await api.post('/api/v1/borrow-records/borrow', {
        bookId,
        borrowerName,
        borrowerPhone
      })
      return new BorrowRecord(response)
    },

    async returnBook(recordId) {
      const response = await api.post(`/api/v1/borrow-records/${recordId}/return`)
      return new BorrowRecord(response)
    },

    async getRecordsByBookId(bookId) {
      const response = await api.get(`/api/v1/borrow-records/book/${bookId}`)
      return response.map(record => new BorrowRecord(record))
    },

    async getRecordsByBorrower(borrowerName) {
      const response = await api.get(`/api/v1/borrow-records/borrower/${borrowerName}`)
      return response.map(record => new BorrowRecord(record))
    },

    async getActiveRecords() {
      const response = await api.get('/api/v1/borrow-records/active')
      return response.map(record => new BorrowRecord(record))
    },

    async getAllRecords() {
      // 获取所有记录，先获取活跃记录，再获取历史记录
      const activeRecords = await this.getActiveRecords()
      // 这里简化处理，实际项目中应该有获取所有记录的API
      return activeRecords
    }
  }
}